//
// Created by 紫霄 on 2020/4/26.
//

#ifndef LEETCODE_58_1_H
#define LEETCODE_58_1_H
#include <sstream>
#include <stack>
#include <string>
using namespace std;
class Solution {
public:
  string reverseWords(string s) {
    if (s.empty()) {
      return s;
    }
    istringstream record(s);
    ostringstream ans;
    stack<string> temp;
    while (!record.eof()) {
      string temStr;
      record >> temStr;
      if (temStr != "  ") {
        temp.emplace(temStr);
      }
    }
    if (temp.top() == "") {
      temp.pop();
    }
    if (!temp.empty()) {
      ans << temp.top();
      temp.pop();
    } else {
      return "";
    }
    while (!temp.empty()) {
      ans << ' ' << temp.top();
      temp.pop();
    }
    return ans.str();
  }
};
#endif // LEETCODE_58_1_H
